#ifndef BW_UPGRADE_H
#define BW_UPGRADE_H

#include <iostream>
#include <vector>
#include <map>
#include <stdbool.h>
#include "bw_public.h"
#include "bw_errno.h"
#include "bw_FcgiRequest.h"

using namespace std;

#ifndef CMD_LEN
#define CMD_LEN 200
#endif
#define UPGRADE_FINISH_FALG    ("UPGRADE_FINISH")       //升级程序结束标志

#define JSONCFG_LOCATION    ("/home/bwsc/upgrade/cfg/encoder.json")
#define TEMP_UPG_XML    	("/home/bwsc/upgrade/cgi/xml/")	        //临时存放提供下载的升级XML配置文件
#define TEMP_OPENSSL_PATH   ("/home/bwsc/upgrade/cgi/openssl/")     //临时存放加密的文件
#define UPGPKG_TMPPATH		("/home/bwsc/upgrade/cgi/pkgPath/")         //临时存放升级包的目录

#define PRODUCT_KEY			"product"
#define PRODUCTNAME_KEY	    "product_name"
#define ENCODER_KEY			"encoder"
#define PKGNAME_KEY			"pkgName"
#define CFG_KEY				"cfg"
#define EXECNUM_KEY         "execFileNum"
#define NORMNUM_KEY         "normFileNum"
#define EXECFILE_KEY		"execFile"
#define NORMALFILE_KEY		"normalFile"
#define MD5_KEY				"md5Value"
#define CFG_NUMBER_KEY	    "cfgNum"
#define INDEX_KEY			"index"
#define UPG_SWITCH_KEY		"upgSwitch"
#define SWITCH_KEY          "switch"
#define FILEPATH_KEY		"filePath"
#define VERSION_KEY			"version"
#define ISREMOTE_KEY		"isRemote"
#define ISADDITION_KEY		"isAddition"
#define FILENAME_KEY		"fileName"
#define PKGNAME_KEY         "pkgName"
#define VERSIONFILE_KEY		"versionFile"
#define ISPROTECT_KEY		"isProtect"
#define PARAM_KEY			"param"
#define RETCOD_KEY          "retCode"
#define RETMSG_KEY          "retMsg"
#define RETDATA_KEY         "retData"
#define SCHEDULE_KEY        "schedule"
#define SUCCESS_KEY         "SUCCESS"
#define MODULE_KEY          "module"
#define REQUEST_KEY         "request"
#define UPGRADE_KEY         "upgrade"
#define SENDFILE_KEY        "sendFile"
#define SET_UPGCFG_KEY	    "setUpgCfg"
#define PIPENAME_KEY        "pipeName"
#define XMLCFGNAME_KEY      "xmlCfgName"
#define UPGRADESERVER_KEY   "upgradeServer"
#define MSGLIST_KEY         "msgList"
#define MSGPATH_KEY         "msgPath"
#define PROJECTID_KEY       "projectId"
#define MSGTYPE_KEY         "msgType"
#define FILEINFO_KEY        "fileInfo"

#define MOD_UPGRADE				  "upgrade"
#define REQ_SET_UPGCFG			  "setUpgCfg"
#define REQ_GET_UPG_PROCESS		  "getUpgProcess"

#define BW_KEY_PASSWORD      "password"
#ifndef KEY_RET_DATA
#define KEY_RET_DATA	    "retData"
#endif
namespace bwcgi
{
    const char* getFileInfo();
    const char* getEncryptPkg(FcgiRequest* pFcgiReq);
    const char* getFileMd5(FcgiRequest *pFcgiReq);

    typedef struct _BW_REPLY_CONTENT_
    {
//        string retCode;
        string retMsg;
        string schedule;
    }PROCESS_REPLY;

    typedef struct BW_EXEC_FILE_
    {
        int		index;
        int		upgSwitch;
        int		isRemote;
        int		isAddition;
        int		isProtected;

        string	fileName;
        string	filePath;
        string	version;
        string	param;
        string	md5Value;
    }EXEC_FILE;

    typedef struct BW_NORM_FILE_
    {
        int		index;
        int		upgSwitch;
        int		isRemote;
        int		isAddition;

        string	fileName;
        string	filePath;
        string	version;
        string	versionFileName;
        string	md5Value;
    }NORM_FILE;

    typedef struct BW_UPG_MSG_
    {
        bool                isInit;
        S32					execNum;
        S32 				normNum;

        vector<EXEC_FILE>	execFileInfo;
        vector<NORM_FILE>	normFileInfo;
    }FILE_INFO;

    typedef struct BW_JSON_CFG_
    {
        bool        isInit;
        string      pipeName;       //管道名
        string      xmlCfgName;     //记录本地文件信息的XML文件
        string      upgradeServer;  //升级程序的完整路径

        string      msgPath;        //消息队列相关参数
        int         projectId;
        int         msgType;
    }BW_JSONCFG;

    class Enc_Upgrade
    {
    public:
        Enc_Upgrade();
        virtual ~Enc_Upgrade();
    public:
        //获取当前文件信息
        eENC_ERROR getFileCfg();

        //获取文件的信息(md5值)
        const char * getSigMd5Value(FcgiRequest *pFcgiReq);

        //设置升级配置，网管会根据配置信息生成xml文件
        eENC_ERROR setUpgCfg(const char *json);

        //下载生成好的xml文件(升级配置)
        eENC_ERROR downloadUpgXml(FcgiRequest* pFcgiReq);

        //加密升级包
        eENC_ERROR opensslUpgPkg(FcgiRequest* pFcgiReq);

        //启动更新程序开始更新, 参数：升级包(3级包)的完整文件名
        const char * beginUpgrade(const string &upgradeFileName);

        //获取更新进度
        const char* getUpgProcess();

        //获取所有软件版本号(MASTER & SLAVE), 用于页面显示
        const char* getVersions();

        //结束升级程序
        eENC_ERROR stopUpgProcess();
        static string respStr;
        static string processStr;
    protected:
        bool getLocalXml();
        bool buildLocalJson();
        bool initJsonCfg();
        bool getUpgJson(const char * json);
        void  buildUpgXml();
        void  InitData();
        const char * buildProcessJson(string key);

        FILE_INFO getLocalFileInfo();   //获取当前文件保存信息的结构体
    private:
        FILE_INFO 	local_data;
        FILE_INFO   upg_data;

        string      upgXmlName;     //只是文件名 不含路径
        string      pkgName;
        string      pkgMd5Value;
        string      upgXmlStr;

        static BW_JSONCFG jsoncfg;
        static const map<string, PROCESS_REPLY> proMsg;
//        static const map<string, string> proMsg;
};

}/*	namespace	*/


#endif
